package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
             //从拦截器中获取当前user_id
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
            //其实查出来肯定是一条数据
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

        //判断当前加入到购物车中的商品是否存在
        //如果存在，number+1
        if(list!=null &&list.size()>0){
            ShoppingCart cart = list.get(0);
            cart.setNumber(cart.getNumber()+1);
            //update shopping_cart set number =？ where id =？
            shoppingCartMapper.updateNumberById(cart);
        }
        else{
            //如果不存在，插入一条购物车数据
            Long dishId = shoppingCart.getDishId();
            if (dishId!=null){
                //本次添加到购物车的是菜品(设置名字，图片，金额，创建时间)
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }else {
                //本次添加到购物车的是套餐(设置名字，图片，金额，创建时间)
                Long setmealId = shoppingCart.getSetmealId();
                Setmeal setmeal = setmealMapper.getById(setmealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());

            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /**
     * 查看购物车
     * @return
     */
    public List<ShoppingCart> showShoppingCart() {
        //获取当前user_id
        Long userId = BaseContext.getCurrentId();
        //一条购物车数据
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(userId)
                .build();
        //当前userid下的所有购物车数据
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        return list;

    }
    /**
     * 清空购物车
     * @return
     */
    public void cleanShoppingCart() {
        //获取当前微信用户的id
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteByUserId(userId);
    }

    /**
     * 删除购物车的一个商品
     * @param shoppingCartDTO
     * @return
     */
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
//      获取当前微信用户的id
        Long userId = BaseContext.getCurrentId();
//      创建 ShoppingCart 对象：把 ShoppingCartDTO 中的属性复制到 ShoppingCart 对象里，并且设置用户 ID。
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(userId)
                .build();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //当前userid下的所选购物车数据
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

//        查询购物车商品信息：利用 shoppingCartMapper.list 方法查询当前商品在购物车中的信息。
        if(list!=null &&list.size()>0){
        //     处理商品数量：
            ShoppingCart cart = list.get(0);
            // 若商品数量大于 1，就把数量减 1 并更新数据库。
            if(cart.getNumber()>1){
                cart.setNumber(cart.getNumber()-1);
                shoppingCartMapper.updateNumberById(cart);
            }else if(cart.getNumber()==1){
            //若商品数量等于 1，就从购物车中移除该商品。
                shoppingCartMapper.deleteById(cart.getId());
            }
        }
    }
}
